package com.cqc.zhbjcqc.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;

import com.cqc.zhbjcqc.R;
import com.cqc.zhbjcqc.activity.MainActivity;
import com.cqc.zhbjcqc.adapter.HomeFragmentPagerAdapter;
import com.cqc.zhbjcqc.view.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import static android.R.id.list;
import static android.R.id.switch_widget;

/**
 * Created by ${cqc} on 2016/9/27.
 */

public class FragHome extends Fragment {
    private Context context;
    private View rootView;
    private NoScrollViewPager viewPager;
    private RadioGroup radioGroup;
    public List<BaseFragment> list = new ArrayList<>();
    private RadioButton tab1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frag_home, container, false);
        findViews();
        initViews();
        return rootView;
    }

    private void initViews() {
        initViewPager();
        initRadioGroup();
    }

    private void initRadioGroup() {
//        tab1.setChecked(true);//默认选中第一个
        radioGroup.check(R.id.tab1);//默认选中第一个
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.tab1:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.tab2:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.tab3:
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.tab4:
                        viewPager.setCurrentItem(3);
                        break;
                    case R.id.tab5:
                        viewPager.setCurrentItem(4);
                        break;
                }
            }
        });
    }

    private void initViewPager() {
        initData();
        HomeFragmentPagerAdapter adapter = new HomeFragmentPagerAdapter(((MainActivity) context).getSupportFragmentManager(), list);
        viewPager.setAdapter(adapter);
    }

    private void initData() {
        list.clear();
        Frag1 frag1 = new Frag1();
        Frag2 frag2 = new Frag2();
        Frag3 frag3 = new Frag3();
        Frag4 frag4 = new Frag4();
        Frag5 frag5 = new Frag5();

        list.add(frag1);
        list.add(frag2);
        list.add(frag3);
        list.add(frag4);
        list.add(frag5);
    }

    private void findViews() {
        viewPager = (NoScrollViewPager) rootView.findViewById(R.id.viewPager_home);
        radioGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup);
        tab1 = (RadioButton) rootView.findViewById(R.id.tab1);
    }

    //获取Frag1 / Frag2 / Frag3 / Frag4 / Frag5给侧拉栏使用
    public BaseFragment getFrag(int position){
        return list.get(position);
    }
}
